Arduino 講習 3
授業内容
レッスン11: ティルトスイッチと液晶ディスプレイをつなぐ
コード例
code: javascript
LiquidCrystal lcd (12,11,5,4,3,2);
int switchState = 0;
int prevSwitchState = 0;
int reply;
void setup() {
// put your setup code here, to run once:
lcd.begin(16,2);
pinMode(6, INPUT);
lcd.print("Ask the");
lcd.setCursor(0,1);
lcd.print("Crystal Ball!");
}
void loop() {
// put your main code here, to run repeatedly:
switchState = digitalRead(6);
if(switchState != prevSwitchState){
if (switchState == LOW){
reply = random(8);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("The Ball says: ");
lcd.setCursor(0,1);
switch(reply){
case 0:
lcd.print("Yes");
break;
case 1:
lcd.print("Most likely");
break;
case 2:
lcd.print("Certainly");
break;
case 3:
lcd.print("Outlook Good");
break;
case 4:
lcd.print("Unsure");
break;
case 5:
lcd.print("Ask again");
break;
case 6:
lcd.print("Doubtful");
break;
case 7:
lcd.print("No");
break;
}
}
}
prevSwitchState = switchState;
}
レッスン13: CapSenseでタッチランプを作る
まずCapacitive Sensing Libraryをダウンロード。Zipを解凍してArduinoのライブラリフォルダに入れる。
Macだと書類の中
WindowsだとユーザフォルダのDocuments
コード例
code: javascript
CapacitiveSensor capSensor = CapacitiveSensor(4,2);
int threshold = 1000;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(12, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
long sensorValue = capSensor.capacitiveSensor(30);
Serial.println(sensorValue);
if (sensorValue > threshold) {
digitalWrite(12, HIGH);
}else{
digitalWrite(12,LOW);
}
}